require('../css/edit.less')
require('../fonts/iconfont.css')


document.ready(function () {
    let sex = document.querySelector('.sex')
    let sexVal = document.querySelector('.sex-val')

    //获取个人数据
    let user = JSON.parse(localStorage.getItem('user'));
    //接收数据对象
    let data = {
        userId: user.userId,
        nickname: '',
        gender: '男',
        birthday: '2020-11-11',
        address: [
            '四川省',
            '成都市'
        ]

    }


    //设置性别
    sex.addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                //渲染
                sexVal.textContent = result[0].label
                //存数据
                data.gender = result[0].label
            },
            title: '请选择性别'
        });
    })

    let birthday = document.querySelector('.birthday')
    let birthdayVal = document.querySelector('.birthday-val')
    //设置生日
    birthday.addEventListener('click', function () {
        weui.datePicker({
            start: 1950,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                console.log(result);
                birthdayVal.textContent = result[0].value + '-' + result[1].value + '-' + result[2].value
                data.birthday = result[0].value + '-' + result[1].value + '-' + result[2].value
                console.log(data);

            },
            title: '请选择日期'
        });
    })


    let pro = document.querySelector('.pro')
    let proVal = document.querySelector('.pro-val')
    let city = document.querySelector('.city')
    let cityVal = document.querySelector('.city-val')
    let cityDataList = []
    //设置省份
    pro.addEventListener('click', function () {
        $http.get('/address/province', function (res) {
            let arr = res.data.map(function (item) {
                return {
                    label: item.name,
                    value: item.addressId
                }
            })
            weui.picker(arr, {
                onConfirm: function (result) {
                    proVal.textContent = result[0].label
                    data.address[0] = result[0].label
                    //清空之前的市级选中数据
                    data.address[1] = ""
                    cityVal.textContent = ''
                    cityList(result[0])

                },
                title: '请选择省份'
            });

        })


    })

    //设置城市
    //在省级回调中 请求的城市列表
    //根据省级id 请求城市列表
    //判断是否有省级id
    function cityList(pro) {
        if (pro.value) {
            $http.get('/address/city/' + pro.value, function (res) {
                cityDataList = res.data.map(function (item) {
                    return {
                        label: item.name,
                        value: item.addressId
                    }
                })
            })
        }
    }

    //点击城市 触发事件--生成picker
    city.addEventListener('click', function (ev) {
        //生成数据
        //生成picker--市级
        weui.picker(cityDataList, {
            onConfirm: function (res) {
                //保存数据
                data.address[1] = res[0].label;
                //渲染页面
                cityVal.textContent = res[0].label

            },
            title: '选择城市'
        });
    })



    //监听返回按钮点击事件
    let back = document.querySelector('.back')

    back.addEventListener('click', function () {
        location.href = '../page/about.html'
    })

    //监听保存按钮点击事件
    // 存储
    let userName = document.querySelector('.user-name')
    let sign = document.querySelector('.sign')
    let save = document.querySelector('.save')
    save.addEventListener('click', function () {
        //拼接数据
        data.sign = sign.value;
        data.nickname = userName.value;
        //生日时间戳
        data.birthday = new Date(data.birthday).getTime();
        //发送请求--修改用户信息
        $http.post('/users/userEdit', data, function (res) {
            console.log(res);

            if (res.status == 0) {
                utils.show(1, '修改信息成功')
                let timeId = setTimeout(function () {
                    location.href = '../page/about.html'
                }, 2000)

            } else {
                utils.show(1, '修改失败');
            }
        })
    })

})